package com.imu.purse.modules.trade.service.impl;

import com.github.pagehelper.PageHelper;
import com.imu.constant.Constant;
import com.imu.core.base.BaseService;
import com.imu.purse.modules.bitcoin.BitcoinApi;
import com.imu.purse.modules.bitcoin.form.BitcoinForm;
import com.imu.purse.modules.trade.dao.FundMapper;
import com.imu.purse.modules.trade.entity.FundEntity;
import com.imu.purse.modules.trade.service.FundService;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-03-31 12:04:14
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class FundServiceImpl extends BaseService<FundEntity> implements FundService 
{
	@Override
    @Transactional
    public int save(final FundEntity fund)
	{
		fund.setTimeCreate(new Date());
    	fund.setTimeUpdate(new Date());
    	return super.insert(fund);
	}
	
	@Override
	@Transactional
    public int update(final FundEntity fund) 
    {
    	fund.setTimeUpdate(new Date());
    	return mapper.updateByPrimaryKeySelective(fund);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final FundEntity fund)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(fund)))
    	{
        	len = this.insert(fund);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}
    	
    	return true;
    }
    
    @Override
    public FundEntity findById(final Long id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(id);
    }
    
    @Override
    public List<FundEntity> queryList(final FundEntity fund)
    {
    	if (fund.getPage() != null && fund.getLimit() != null)
    	{
            PageHelper.startPage(fund.getPage(), fund.getLimit());
        }
    	
    	return mapper.findByPage(fund);
    }

	@Transactional
    public int upMoney(final FundEntity entity){
    	return mapper.upMoney(entity);
    }
    
    @Override
	public FundEntity findByUserId(Long userId) {
		return mapper.findByUserId(userId);
	}
    
    @Override
	@Transactional
    public int updateMoneyByUIdAndYtype(final FundEntity entity)
    {
    	entity.setTimeUpdate(new Date());
    	return mapper.updateMoneyByUIdAndYtype(entity);
    }
    
    @Override
	@Transactional
    public int upPri(final FundEntity entity)
    {
    	entity.setTimeUpdate(new Date());
    	return mapper.upPri(entity);
    }
    
    @Override
   	@Transactional
    public int up(final FundEntity entity)
    {
    	entity.setTimeUpdate(new Date());
    	return mapper.up(entity);
    }
    


	@Override
	public String transMoney(BitcoinForm form) {
		/*CoreMoneyAddr c=new CoreMoneyAddr();
		c.setUrl(form.getAddress());*/
		String num = bitcoinApi.sendfrom("13872389146", form.getAccount(), form.getAmount(), form.getAddress());
		return num;
	}

	public Long findUserIdByAdress(String address){
		Object cacheUserId = redisTemplate.opsForHash().get(Constant.CORE_MONEY_USER_KEY,address);
		if(cacheUserId == null || "null".equals(cacheUserId.toString())){
			cacheUserId = null;
			FundEntity fundEntity = new FundEntity();
			fundEntity.setMoneyAddress(address);
			fundEntity = mapper.selectOne(fundEntity);
			if(fundEntity != null){
				redisTemplate.opsForHash().put(Constant.CORE_MONEY_USER_KEY,address,fundEntity.getUserId().toString());
			}
		}
		return cacheUserId == null ? null : Long.valueOf(cacheUserId.toString());
	}

	@Autowired
	private FundMapper mapper;

	@Autowired
	private BitcoinApi bitcoinApi;

	@Autowired
	private RedisTemplate<String,String> redisTemplate;
}
